/*
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package ru.irenproject;

import ru.irenproject.classifyQuestion.ClassifyQuestion;
import ru.irenproject.classifyQuestion.ClassifyQuestionEditor;
import ru.irenproject.classifyQuestion.ClassifyQuestionProtos;
import ru.irenproject.classifyQuestion.ClassifyQuestionReader;
import ru.irenproject.classifyQuestion.ClassifyQuestionWriter;
import ru.irenproject.inputQuestion.AbstractPatternEditor;
import ru.irenproject.inputQuestion.InputQuestion;
import ru.irenproject.inputQuestion.InputQuestionEditor;
import ru.irenproject.inputQuestion.InputQuestionProtos;
import ru.irenproject.inputQuestion.InputQuestionReader;
import ru.irenproject.inputQuestion.InputQuestionWriter;
import ru.irenproject.inputQuestion.Pattern;
import ru.irenproject.inputQuestion.RegexpPattern;
import ru.irenproject.inputQuestion.RegexpPatternEditor;
import ru.irenproject.inputQuestion.TextPattern;
import ru.irenproject.inputQuestion.TextPatternEditor;
import ru.irenproject.it.It;
import ru.irenproject.it.ItReaderFactory;
import ru.irenproject.it.ItWriterFactory;
import ru.irenproject.it.ModifierReader;
import ru.irenproject.it.ModifierWriter;
import ru.irenproject.it.QuestionReader;
import ru.irenproject.it.QuestionWriter;
import ru.irenproject.matchQuestion.MatchQuestion;
import ru.irenproject.matchQuestion.MatchQuestionEditor;
import ru.irenproject.matchQuestion.MatchQuestionProtos;
import ru.irenproject.matchQuestion.MatchQuestionReader;
import ru.irenproject.matchQuestion.MatchQuestionWriter;
import ru.irenproject.modifier.AddNegativeChoiceModifier;
import ru.irenproject.modifier.AddNegativeChoiceModifierReader;
import ru.irenproject.modifier.AddNegativeChoiceModifierWriter;
import ru.irenproject.modifier.ModifierProtos;
import ru.irenproject.modifier.ScriptModifier;
import ru.irenproject.modifier.ScriptModifierReader;
import ru.irenproject.modifier.ScriptModifierWriter;
import ru.irenproject.modifier.SetEvaluationModelModifier;
import ru.irenproject.modifier.SetEvaluationModelModifierReader;
import ru.irenproject.modifier.SetEvaluationModelModifierWriter;
import ru.irenproject.modifier.SetNegativeChoiceContentModifier;
import ru.irenproject.modifier.SetNegativeChoiceContentModifierReader;
import ru.irenproject.modifier.SetNegativeChoiceContentModifierWriter;
import ru.irenproject.modifier.SetWeightModifier;
import ru.irenproject.modifier.SetWeightModifierReader;
import ru.irenproject.modifier.SetWeightModifierWriter;
import ru.irenproject.modifier.ShuffleChoicesModifier;
import ru.irenproject.modifier.ShuffleChoicesModifierReader;
import ru.irenproject.modifier.ShuffleChoicesModifierWriter;
import ru.irenproject.modifier.SuppressSingleChoiceHintModifier;
import ru.irenproject.modifier.SuppressSingleChoiceHintModifierReader;
import ru.irenproject.modifier.SuppressSingleChoiceHintModifierWriter;
import ru.irenproject.orderQuestion.OrderQuestion;
import ru.irenproject.orderQuestion.OrderQuestionEditor;
import ru.irenproject.orderQuestion.OrderQuestionProtos;
import ru.irenproject.orderQuestion.OrderQuestionReader;
import ru.irenproject.orderQuestion.OrderQuestionWriter;
import ru.irenproject.pad.PadProtos;
import ru.irenproject.selectQuestion.SelectQuestion;
import ru.irenproject.selectQuestion.SelectQuestionEditor;
import ru.irenproject.selectQuestion.SelectQuestionProtos;
import ru.irenproject.selectQuestion.SelectQuestionReader;
import ru.irenproject.selectQuestion.SelectQuestionWriter;
import ru.irenproject.store.StoreFactory;

import com.google.inject.AbstractModule;
import com.google.inject.TypeLiteral;
import com.google.inject.assistedinject.FactoryModuleBuilder;
import com.google.inject.multibindings.MapBinder;
import com.google.inject.multibindings.Multibinder;
import com.google.protobuf.Descriptors.FileDescriptor;

public final class MainModule extends AbstractModule {
  @Override protected void configure() {
    registerProtoDescriptors();
    registerQuestionEditors();
    registerQuestionBriefFactories();
    registerPatternEditors();
    registerQuestionWriters();
    registerQuestionReaders();
    install(new FactoryModuleBuilder().build(ItWriterFactory.class));
    install(new FactoryModuleBuilder().build(ItReaderFactory.class));
    registerModifierWriters();
    registerModifierReaders();
    install(new FactoryModuleBuilder().build(StoreFactory.class));
  }

  private void registerProtoDescriptors() {
    Multibinder<FileDescriptor> b = Multibinder.newSetBinder(binder(), FileDescriptor.class);
    for (FileDescriptor d : new FileDescriptor[] {
        BaseProtos.getDescriptor(),
        PadProtos.getDescriptor(),
        SelectQuestionProtos.getDescriptor(),
        InputQuestionProtos.getDescriptor(),
        MatchQuestionProtos.getDescriptor(),
        OrderQuestionProtos.getDescriptor(),
        ClassifyQuestionProtos.getDescriptor(),
        TestProtos.getDescriptor(),
        UiProtos.getDescriptor(),
        ModifierProtos.getDescriptor()
        }) {
      b.addBinding().toInstance(d);
    }
  }

  private void registerQuestionEditors() {
    MapBinder<Class<? extends Question>, Class<? extends AbstractQuestionEditor>>
        b = MapBinder.newMapBinder(binder(),
            new TypeLiteral<Class<? extends Question>>() {},
            new TypeLiteral<Class<? extends AbstractQuestionEditor>>() {});
    b.addBinding(SelectQuestion.class).toInstance(SelectQuestionEditor.class);
    b.addBinding(InputQuestion.class).toInstance(InputQuestionEditor.class);
    b.addBinding(MatchQuestion.class).toInstance(MatchQuestionEditor.class);
    b.addBinding(OrderQuestion.class).toInstance(OrderQuestionEditor.class);
    b.addBinding(ClassifyQuestion.class).toInstance(ClassifyQuestionEditor.class);
  }

  private void registerQuestionBriefFactories() {
    MapBinder<Class<? extends Question>, QuestionBriefFactory>
        b = MapBinder.newMapBinder(binder(),
            new TypeLiteral<Class<? extends Question>>() {},
            new TypeLiteral<QuestionBriefFactory>() {});
    b.addBinding(SelectQuestion.class).to(FormulationBriefFactory.class);
    b.addBinding(InputQuestion.class).to(FormulationBriefFactory.class);
    b.addBinding(MatchQuestion.class).to(FormulationBriefFactory.class);
    b.addBinding(OrderQuestion.class).to(FormulationBriefFactory.class);
    b.addBinding(ClassifyQuestion.class).to(FormulationBriefFactory.class);
  }

  private void registerPatternEditors() {
    MapBinder<Class<? extends Pattern>, Class<? extends AbstractPatternEditor>>
        b = MapBinder.newMapBinder(binder(),
            new TypeLiteral<Class<? extends Pattern>>() {},
            new TypeLiteral<Class<? extends AbstractPatternEditor>>() {});
    b.addBinding(TextPattern.class).toInstance(TextPatternEditor.class);
    b.addBinding(RegexpPattern.class).toInstance(RegexpPatternEditor.class);
  }

  private void registerQuestionWriters() {
    MapBinder<Class<? extends Question>, QuestionWriter>
        b = MapBinder.newMapBinder(binder(),
            new TypeLiteral<Class<? extends Question>>() {},
            new TypeLiteral<QuestionWriter>() {});
    b.addBinding(SelectQuestion.class).to(SelectQuestionWriter.class);
    b.addBinding(InputQuestion.class).to(InputQuestionWriter.class);
    b.addBinding(MatchQuestion.class).to(MatchQuestionWriter.class);
    b.addBinding(OrderQuestion.class).to(OrderQuestionWriter.class);
    b.addBinding(ClassifyQuestion.class).to(ClassifyQuestionWriter.class);
  }

  private void registerQuestionReaders() {
    MapBinder<String, QuestionReader>
        b = MapBinder.newMapBinder(binder(),
            new TypeLiteral<String>() {},
            new TypeLiteral<QuestionReader>() {});
    b.addBinding(It.VAL_QUESTION_TYPE_SELECT).to(SelectQuestionReader.class);
    b.addBinding(It.VAL_QUESTION_TYPE_INPUT).to(InputQuestionReader.class);
    b.addBinding(It.VAL_QUESTION_TYPE_MATCH).to(MatchQuestionReader.class);
    b.addBinding(It.VAL_QUESTION_TYPE_ORDER).to(OrderQuestionReader.class);
    b.addBinding(It.VAL_QUESTION_TYPE_CLASSIFY).to(ClassifyQuestionReader.class);
  }

  private void registerModifierWriters() {
    MapBinder<Class<? extends Modifier>, ModifierWriter>
        b = MapBinder.newMapBinder(binder(),
            new TypeLiteral<Class<? extends Modifier>>() {},
            new TypeLiteral<ModifierWriter>() {});
    b.addBinding(ShuffleChoicesModifier.class).to(ShuffleChoicesModifierWriter.class);
    b.addBinding(AddNegativeChoiceModifier.class).to(AddNegativeChoiceModifierWriter.class);
    b.addBinding(SuppressSingleChoiceHintModifier.class).to(SuppressSingleChoiceHintModifierWriter.class);
    b.addBinding(SetEvaluationModelModifier.class).to(SetEvaluationModelModifierWriter.class);
    b.addBinding(ScriptModifier.class).to(ScriptModifierWriter.class);
    b.addBinding(SetNegativeChoiceContentModifier.class).to(SetNegativeChoiceContentModifierWriter.class);
    b.addBinding(SetWeightModifier.class).to(SetWeightModifierWriter.class);
  }

  private void registerModifierReaders() {
    MapBinder<String, ModifierReader>
        b = MapBinder.newMapBinder(binder(),
            new TypeLiteral<String>() {},
            new TypeLiteral<ModifierReader>() {});
    b.addBinding(It.VAL_MODIFIER_TYPE_SHUFFLE_CHOICES).to(ShuffleChoicesModifierReader.class);
    b.addBinding(It.VAL_MODIFIER_TYPE_ADD_NEGATIVE_CHOICE).to(AddNegativeChoiceModifierReader.class);
    b.addBinding(It.VAL_MODIFIER_TYPE_SUPPRESS_SINGLE_CHOICE_HINT).to(SuppressSingleChoiceHintModifierReader.class);
    b.addBinding(It.VAL_MODIFIER_TYPE_SET_EVALUATION_MODEL).to(SetEvaluationModelModifierReader.class);
    b.addBinding(It.VAL_MODIFIER_TYPE_SCRIPT).to(ScriptModifierReader.class);
    b.addBinding(It.VAL_MODIFIER_TYPE_SET_NEGATIVE_CHOICE_CONTENT).to(SetNegativeChoiceContentModifierReader.class);
    b.addBinding(It.VAL_MODIFIER_TYPE_SET_WEIGHT).to(SetWeightModifierReader.class);
  }
}
